<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>受控组件</title>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
</head>
<body>
    <div id="app"></div>
    <script type="text/babel">
        class LoginForm extends React.Component {
            state = {
                username: '',
                password: ''
            }
            render(){
                return (
                    <form onSubmit={this.submitForm}>
                        用户名：<input type="text" onChange={this.typingUsername}/>
                        密码：<input type="password" onChange={this.typingPassword}/>
                        <button>提交</button>
                    </form>
                )
            }

            typingUsername = (event) => { // 通过回调函数会传入事件对象这个特性，从而拿到事件目标上的属性值
                this.setState({ //将表单属性更新到React 组件状态 state 中
                    username: event.target.value
                })
            }

            typingPassword = (event) => {
                this.setState({
                    password: event.target.value
                })
            }

            submitForm = (event) => {
                event.preventDefault();
                const {username, password} = this.state //从状态中取得表单状态
                alert(`username: ${username}  password: ${password}`)
            }
        }

        ReactDOM.render(<LoginForm/>, document.querySelector('#app'))
    </script>
</body>
</html>